package 链表;

import common.ListNode;


/**
 * @author tmh
 * @date 2024/11/22 19:22
 * @description
 */
public class T2两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode l3 = new ListNode();
        ListNode p = l3;
        int count = 0;
        while (l1 != null || l2 != null || count != 0) {
            int num3 = 0;
            if (l1 != null) {
                num3 += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                num3 += l2.val;
                l2 = l2.next;
            }
            num3 += count;
            count = num3 >= 10 ? num3 / 10 : 0;
            l3.next = new ListNode(num3 % 10);
            l3 = l3.next;
        }
        return p.next;
    }
}
